package com.ndf.learn.controller;

import com.ndf.learn.aop.CryptoTag;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lin on 2017/11/18.
 */
@Controller
public class HelloController {

    @RequestMapping(value = {"/hello"} , method = {RequestMethod.GET})
    public ModelAndView hello(HttpServletRequest request
            , HttpServletResponse response) throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "Hello World");
        mv.setViewName("hello.jsp");
        return mv;
    }

    @CryptoTag(value = true)
    @RequestMapping(value = {"/testJson", "/testJsonB"}, method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody Map testJson(
            @RequestParam Map<String, Object> paramMap
            , HttpServletRequest request
            , HttpServletResponse response
    ){
        Map<String, Object> resultMap = new HashMap<String, Object>(16);
        resultMap.put("a", "123");
        resultMap.put("b", "234");
        resultMap.put("c", "345");

        if(paramMap != null && !paramMap.isEmpty()){
            resultMap.putAll(paramMap);
        }
        return resultMap;
    }

    @RequestMapping(value = "/testFreemarker")
    @ResponseBody
    public ModelAndView testFreemarker(@RequestParam(value = "a", required = false) String a) throws Exception {
        ModelAndView mv = new ModelAndView();

        List<String> list = new ArrayList<String>();
        list.add("Jack1");
        list.add("Jack2");
        list.add("Jack3");

        mv.addObject("list", list);
        mv.addObject("val", a);
        mv.setViewName("testFreemarker.ftl");
        return mv;
    }

    @RequestMapping(value = "/testVelocity", method = RequestMethod.GET)
    public ModelAndView testVelocity() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("hello", "test velocity");
        mv.setViewName("testVelocity.vm");
        return mv;
    }

}
